<?php
abstract class View {
	
	public $vars;
	public $system;
	protected $helpers_to_load;
	protected $url_header="";
	protected $url_footer="";
	protected $url_template="";
	protected $output = "";
	protected $smartphone = false;
	protected $arr_tpl_mobile = array();
	protected $output_var;
	
	protected $css = "";
	protected $js = "";
	
	protected $viola_ajax_support= false;
	
	private $cache;
	
	/**
	 * Observers caricati del model
	 * @var unknown_type
	 */
	public $observers = array();
	
	
	function __construct(System $system){
		$this->system = &$system;
		$this->vars = $this->system->view_vars;
		$this->observers = $this->system->view_observers;
		
		if(VIOLA_CACHE){
			$this->cache = MVC::load('Cache');
		}
		
		if(MVC::FunctionExists($this->system->mvc_action['action'], $this)){
			$ob = get_class($this);
			call_user_func_array(array($ob, $this->system->mvc_action['action']), array());
		} else {
			$this->init();
		}
	}
	
	/**
	 * String replace an associative array of comments
	 *
	 * @param array $array
	 */
	protected function sostituisci_array($array){
			
		foreach($array as $commento => $stringa){
				
			$this->sostituisci("<!-- $commento !-->",$stringa);
				
		}
	}
	
	/**
	 * Carica gli helpers
	 */
	private function LoadHelpers(){
		if(!empty($this->helpers_to_load)){
			foreach($this->helpers_to_load as $helper){
				$this->system->helpers[$helper] = MVC::load($helper);
			}
		}
	}
	
	
	/**
	 * String replace a file
	 *
	 * @param string $commento
	 * @param string $file
	 */
	protected function sostituisci_file($commento,$file){
		$this->sostituisci("<!-- $commento !-->",file_get_contents($file));
	}
	
	/**
	 * String replace of comments
	 *
	 * @param string $commento
	 * @param string $stringa
	 */
	protected function sostituisci($commento="",$stringa=""){
	
		if(is_string($stringa))$this->output = str_replace("<!-- $commento !-->", $stringa, $this->output);
		if(is_array($stringa))$this->output = str_replace("<!-- $commento !-->", implode($stringa), $this->output);
	}
	
	protected function sostituisci_in_var($commento="",$stringa=""){
	
		if($this->smartphone){
			foreach($this->arr_tpl_mobile as $tpl){
				$stringa = str_replace($tpl['original'],$tpl['new'],$stringa);
			}
		}
	
		if(is_string($stringa))$this->output_var = str_replace("<!-- $commento !-->", $stringa, $this->output_var);
		if(is_array($stringa))$this->output_var = str_replace("<!-- $commento !-->", implode($stringa), $this->output_var);
	}
	
	protected function sostituisci_in_var_file($commento,$file){
	
		if($this->smartphone){
			$file = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$file);
		}
	
		$this->sostituisci_in_var($commento,file_get_contents($file));
	}
	

	/**
	 * Stampa l'output della pagina
	 *
	 */
	protected function output() {
		
		if(!DEBUG){
			ob_start('ob_gzhandler') ;
			header("Content-Encoding: gzip");
			header("Content-type: text/html; charset=utf-8");
		}
		if($this->system->request->json_response || $this->system->request->ajax){
			echo json_encode($this->output);
		} else {
			if(VIOLA_CACHE){
				$this->cache->verify($this);
				if(empty($this->cache->cache_contents)){
					$this->cache->cachize($this->output);
				}
			}
			echo $this->output;
		}
	}
	
	protected function read_template(){
		$this->output = "";
		 
		if($this->url_header){ $this->output .= file_get_contents($this->url_header); }
		$this->output .= file_get_contents($this->url_template);
		if($this->url_footer){ $this->output .= file_get_contents($this->url_footer); }
	
		if(defined('TEMPLATE'))$this->sostituisci("TEMPLATE_DIR",TEMPLATE);
		if(defined('TEMPLATE_IMAGES_DIR'))$this->sostituisci("TEMPLATE_IMAGES_DIR",TEMPLATE_IMAGES_DIR);
		if(!empty($this->css)){
			if(is_array($this->css))$this->sostituisci_array_css();
			else $this->sostituisci_css();
		}
		if(!empty($this->js)){
			if(is_array($this->js))$this->sostituisci_array_js();
			else $this->sostituisci_js();
		}
		if($this->viola_ajax_support){
			$this->footer_js[] = JS_DIR."viola.js";	
		}
		if(!empty($this->footer_js)){
			if(is_array($this->footer_js))$this->sostituisci_footer_array_js();
			else $this->sostituisci_footer_js();
		}
	}
	
	private function sostituisci_array_css(){
		$nr = count($this->css);
		for($i = 0; $i < $nr; $i++){
			$css_line = '<link rel="stylesheet" href="'.$this->css[$i].'" media="screen" type="text/css" />';
			$css_line.= $i + 1 == $nr ? '<!-- CSS !-->' : '';
		}
		$this->sostituisci("CSS", $css_line);
	}
	
	private function sostituisci_array_js(){
		$nr = count($this->js);
		for($i = 0; $i < $nr; $i++){
			$js_line = '<script type="text/javascript" src="'.$this->js[$i].'"></script>';
			$js_line.= $i + 1 == $nr ? '<!-- JS !-->' : '';
		}
		$this->sostituisci("JS", $js_line);
	}
	
	private function sostituisci_footer_array_js(){
		$nr = count($this->footer_js);
		for($i = 0; $i < $nr; $i++){
			$fjs_line = '<script type="text/javascript" src="'.$this->footer_js[$i].'"></script>';
			$fjs_line.= $i + 1 == $nr ? '<!-- FOOTER_JS !-->' : '';
		}
		if($this->viola_ajax_support){
			$fjs_line.= '<input type="hidden" name="viola_ajax" value="<!-- VIOLA_AJAX_VALUE !-->" />';
		}
		$this->sostituisci("FOOTER_JS", $fjs_line);
	}
	
	private function sostituisci_css(){
		$css_line = '<link rel="stylesheet" href="'.$this->css.'" media="screen" type="text/css" />';
		$this->sostituisci("CSS", $css_line);
	}
	
	private function sostituisci_js(){
		$js_line = '<script type="text/javascript" src="'.$this->js.'"></script>';
		$this->sostituisci("JS", $js_line);
	}
	
	private function sostituisci_footer_js(){
		$fjs_line = '<script type="text/javascript" src="'.$this->footer_js.'"></script>';
		$this->sostituisci("FOOTER_JS", $fjs_line);
	}
	
	protected function SetSystemEvent($event){
		$this->system->model->SetSystemEvent($event);
	}
	
	/**
	 * Distruttotre, se ci sono observers li esegue prima di chiudere la classe.
	 */
	public function __destruct(){
		if(!empty($this->observers)){
			$nr = count($this->observers);
			for($i = 0; $i < $nr; $i++){
				$this->observers[$i]->control($this);
			}
		}
	}

}